package com.kwedding.service.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.kwedding.bean.TB_Account;
import com.kwedding.bean.TB_City;
import com.kwedding.dao.TB_AccountMapper;
import com.kwedding.dao.TB_CityMapper;
import com.kwedding.dto.web.RequestWebAccountList;
import com.kwedding.dto.web.RequestWebAddAccount;
import com.kwedding.dto.web.RequestWebUpDownAccount;
import com.kwedding.dto.web.RequestWebUpdateAccount;
import com.kwedding.dto.web.ResponseWebAccountList;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyPageInfo;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.IDGenerate;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;
import com.kwedding.util.Resp_PageInfo;

@Service
public class WebAccountService extends LoggerParent{

	@Autowired
	private TB_AccountMapper tb_AccountMapper;
	@Autowired
	private TB_CityMapper tb_CityMapper;

	/**
	 * 添加用户账号
	 * 
	 * @param webAddAccount
	 */
	@Transactional
	public void insertAccount(RequestWebAddAccount webAddAccount) {
		CheckedUtil.isNull(webAddAccount.getCityId(), Constants.REQUEST_ERROR_1, "城市不能为空");
		CheckedUtil.isNull(webAddAccount.getAvator(), Constants.REQUEST_ERROR_2, "头像不能为空");
		CheckedUtil.isNull(webAddAccount.getName(), Constants.REQUEST_ERROR_3, "账号拥有者不能为空");
		CheckedUtil.isNull(webAddAccount.getPassword(), Constants.REQUEST_ERROR_4, "密码不能为空");
		CheckedUtil.isNull(webAddAccount.getPhone(), Constants.REQUEST_ERROR_5, "手机号不能为空");
		TB_Account tb_Account = CopyerObject.copyerObject(webAddAccount, TB_Account.class);
		TB_City tb_City = tb_CityMapper.selectByPrimaryKey(webAddAccount.getCityId());
		if (tb_City == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_6, "城市选择错误");
		}
		tb_Account.setAccountId(IDGenerate.generate());
		String userName = null;
		// 根据城市名称以及用户数生成账号
		int i = tb_AccountMapper.getAccountNumByCityId(webAddAccount.getCityId());
		if (String.valueOf(i + 1).length() == 1) {
			userName = "Kwedding000" + (i + 1);
		}
		if (String.valueOf(i + 1).length() == 2) {
			userName = "Kwedding00" + (i + 1);
		}
		if (String.valueOf(i + 1).length() == 3) {
			userName = "Kwedding0" + (i + 1);
		}
		if (String.valueOf(i + 1).length() == 4) {
			userName = "Kwedding" + (i + 1);
		}
		tb_Account.setUserName(userName);
		tb_Account.setCreateTime(new Date());
		tb_Account.setUpdateTime(new Date());
		tb_Account.setStatus(false);
		if (tb_AccountMapper.insert(tb_Account) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_7, "添加用户失败");
		}
	}

	/**
	 * 获取用户列表
	 * 
	 * @param requestWebAccountList
	 * @return
	 */
	@Transactional(readOnly = true)
	public Resp_PageInfo<ResponseWebAccountList> getAccountList(RequestWebAccountList requestWebAccountList) {
		int page = 1;
		if (requestWebAccountList.getPageNum() != null) {
			page = requestWebAccountList.getPageNum();
		}
		PageHelper.startPage(page, Constants.PAGESIZE);
		List<TB_Account> tb_Accounts = tb_AccountMapper.selectAccountListByCityId(requestWebAccountList.getSearchName(),
				requestWebAccountList.getStatus(), requestWebAccountList.getCityId());
		List<ResponseWebAccountList> webAccountLists = new ArrayList<>();
		if (tb_Accounts != null && tb_Accounts.size() > 0) {
			tb_Accounts.stream().forEach(r -> {
				ResponseWebAccountList accountList = CopyerObject.copyerObject(r, ResponseWebAccountList.class);
				accountList.setIdStr(r.getAccountId().toString());
				webAccountLists.add(accountList);
			});
		}
		return CopyPageInfo.copyPageInfo(tb_Accounts, webAccountLists);

	}

	/**
	 * 修改用户信息
	 * 
	 * @param webUpdateAccount
	 */
	@Transactional
	public void upDateAccount(RequestWebUpdateAccount webUpdateAccount) {
		CheckedUtil.isNull(webUpdateAccount.getCityId(), Constants.REQUEST_ERROR_1, "城市代码不能为空");
		CheckedUtil.isNull(webUpdateAccount.getAvator(), Constants.REQUEST_ERROR_2, "头像不能为空");
		CheckedUtil.isNull(webUpdateAccount.getName(), Constants.REQUEST_ERROR_3, "账号拥有者不能为空");
		CheckedUtil.isNull(webUpdateAccount.getPassword(), Constants.REQUEST_ERROR_4, "密码不能为空");
		CheckedUtil.isNull(webUpdateAccount.getPhone(), Constants.REQUEST_ERROR_5, "手机号不能为空");
		CheckedUtil.isNull(webUpdateAccount.getAccountId(), Constants.REQUEST_ERROR_6, "请选择要修改的用户");
		TB_Account account = tb_AccountMapper.selectbyPrimaryKey(webUpdateAccount.getAccountId());
		if (account == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_7, "用户信息不存在或者已删除");
		}
		account = CopyerObject.copyerObject(webUpdateAccount, TB_Account.class);
		account.setUpdateTime(new Date());
		if (tb_AccountMapper.updateSelective(account) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_8, "修改用户信息失败");
		}
	}

	/***
	 * 下架用户
	 * 
	 * @param downAccount
	 */
	@Transactional
	public void upDownAccount(RequestWebUpDownAccount downAccount) {
		CheckedUtil.isNull(downAccount.getAccountId(), Constants.REQUEST_ERROR_1, "请选择要修改的用户");
		TB_Account tb_Account = tb_AccountMapper.selectbyPrimaryKey(downAccount.getAccountId());
		if (tb_Account == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "用户信息不存在或者已删除");
		}
		tb_Account.setStatus(!tb_Account.getStatus());
		tb_Account.setUpdateTime(new Date());
		if (tb_AccountMapper.updateSelective(tb_Account) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "修改用户信息失败");
		}
	}
}
